package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 售货机微服务出货协议
 * 获取订单微服务消息
 * 修改库存
 * 记录一个流水，用来记录，售货机终端出货是否成功
 */
@Topic(TopicConfig.VMS_VENDOUT_TOPIC)
@Slf4j
@Component
public class VendoutMsgHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;


    @Override
    public void process(String jsonMsg) throws IOException {
        //解析消息
        VendoutContract vendoutContract = JsonUtil.getByJson(jsonMsg, VendoutContract.class);
        //售货机号非空判断
        if (StringUtils.isBlank(vendoutContract.getInnerCode())) return;
        //逻辑处理,修改库存,记录流水
        vendingMachineService.vendout(vendoutContract);

    }
}
